#Notes: 
#	Problems with translating (uploading pictures and saving translated text):
#		It doesn't work with API
#    	It doesn't work with clicker

import requests
import lxml.html as html
import cssselect
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import config
import time
import webbrowser


user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':user_agent,} 

sessionObj = requests.session()
url = "https://translate.yandex.com/ocr"


driver = webdriver.Firefox()
driver.get(url)

values = {
    "button button_tab state-selected" : "Image",
    "button button_select button_select_source" : "Korean",
    "button button_select button_select_target" : "Russian",
   }

r = sessionObj.post(url, data=values)

time.sleep(2)
el = driver.find_element_by_link_text("Select file")
el.click()
time.sleep(4)

picture_name = 191
pic_n = 1


print (r.status_code) #//msg:'200'

element = driver.find_element_by_type("file")
element.send_keys("/root/Pictures/try_to_translate_photo/pic/191_1.jpg")
time.sleep(6)

try:
    element1 = wait.until(EC.element_to_be_clickable(By.text("Open in Yandex.Translate")))  
    element1.click();
except (Exception, e):
    print("error")
time.sleep(4)

driver.close()